The ggfortify package makes it very easy to plot time series directly from a time series object, without having to convert it to a dataframe. The example below plots the AirPassengers timeseries in one step.
6.1 Make a time series plot (using ggfortify)
autoplot(AirPassengers) +
labs(title="AirPassengers") # where AirPassengers is a 'ts' object

6.2 Plot multiple timeseries on same ggplot
# Approach 1:
data(economics, package="ggplot2") # init data
economics <- data.frame(economics) # convert to dataframe
ggplot(economics) + geom_line(aes(x=date, y=pce, color="pcs")) +
geom_line(aes(x=date, y=unemploy, col="unemploy")) +
scale_color_discrete(name="Legend") + labs(title="Economics") # plot multiple time series using 'geom_line's

# Approach 2:
library(reshape2)
df <- melt(economics[, c("date", "pce", "unemploy")], id="date")
ggplot(df) + geom_line(aes(x=date, y=value, color=variable)) +
labs(title="Economics")# plot multiple time series by melting

Multiple Y-axis on the same plot : Multiple time series on the same scale can make few of the series appear small. An alternative would be to facet_wrap it and set the scales='free'.
df <- melt(economics[, c("date", "pce", "unemploy", "psavert")], id="date")
ggplot(df) + geom_line(aes(x=date, y=value, color=variable)) +
facet_wrap( ~ variable, scales="free")

6.3 ~ 6.5 Bar charts (Exercises!)
6.6 Adjust X and Y axis limits
There are 3 ways to change the X and Y axis limits.
- Using
coord_cartesian(xlim=c(x1,x2))
- Using
xlim(c(x1,x2))
- Using
scale_x_continuous(limits=c(x1,x2))
- Warning: 2., 3. will delete the datapoints that lie outisde the limit and 1. does not delete any datapoint. But instead it zooms in to a specific region of the chart.
ggplot(diamonds, aes(x=carat, y=price, color=cut)) +
geom_point() + geom_smooth() + coord_cartesian(ylim=c(0, 10000)) +
labs(title="Coord_cartesian zoomed in!")
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point() +
geom_smooth() + ylim(c(0, 10000)) +
labs(title="Datapoints deleted: Note the change in smoothing lines!")
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
## Warning: Removed 5222 rows containing non-finite values (stat_smooth).
## Warning: Removed 5222 rows containing missing values (geom_point).

6.7 Equal coordinates
Apart from the basic ggplot2 theme, you can change the look and feel of your plots using one of these builtin themes.
-theme_gray()
-theme_bw()
-theme_linedraw()
-theme_light()
-theme_minimal()
-theme_classic()
-theme_void()
The ggthemes package provides additional ggplot themes that imitates famous magazines and softwares.
ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point() +
geom_smooth() +theme_bw() + labs(title="bw Theme")
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

6.9 Legend - Deleting and Changing Position
By setting theme(legend.position="none"), you can remove the legend.
By setting it to ‘top’, you can move the legend around the plot.
By setting legend.postion to a co-ordinate inside the plot you can place the legend inside the plot itself.
The legend.justification denotes the the point that will be placed on the co-ordinates given by legend.position.
p1 <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) +
geom_point() + geom_smooth() + theme(legend.position="none") +
labs(title="legend.position='none'") # remove legend
p2 <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) +
geom_point() + geom_smooth() + theme(legend.position="top") +
labs(title="legend.position='top'") # at top
p3 <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point() +
geom_smooth() + labs(title="legend.position='coords inside plot'") +
theme(legend.justification=c(1,0), legend.position=c(1,0)) # inside the plot
gridExtra::grid.arrange(p1, p2, p3, ncol=3) # arrange
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

6.12 Annotation

6.13 Saving ggplot
plot1 <- ggplot(mtcars, aes(x=cyl)) + geom_bar()
ggsave("myggplot.png") # saves the last plot.
ggsave("myggplot.png", plot=plot1) # save a stored ggplot